<?php

include_once ('Logging.php');
include_once ('Data.php');

class POSClass
{
    # 推送
    const URL = 'http://58.40.18.134/QTinyMall/servlet/TinyFuncServlet';//'http://116.228.73.123/QTinyMall/servlet/TinyFuncServlet';
    const URL_TEST = 'http://if-dev.map-s.cn/QTinyMallGANSO/servlet/TinyFuncServlet';

    # KEY
    const KEY_URL_TEST = 'http://if-dev.map-s.cn/KeyServletGANSO/servlet/GetKeyServlet';
    const KEY_URL = 'http://58.40.18.134/KeyServlet/servlet/GetKeyServlet';

    const IF_AC = 'ZP_RSVP';                   # if_ac
    const MAPS_UID = 'MAPS_GANSO';                # maps_uid
    const IF_MOD = 'GANSO_DELI_SET';                  # if_mod

    const IF_AC_TEST = 'GANSO_DELI';
    const MAPS_UID_TEST = 'MAPS_GANSO_DEV';
    const IF_MOD_TEST = 'GANSO_ZP_DELI_SET';


    private $isDebug = false;

    public function __construct($flag = true)
    {
        $this->isDebug = $flag;
    }

    public function GetKey()
    {
        $if_ac = (($this->isDebug) ? self::IF_AC_TEST : self::IF_AC);
        $maps_uid = (($this->isDebug) ? self::MAPS_UID_TEST : self::MAPS_UID);
        $url = (($this->isDebug) ? self::KEY_URL_TEST : self::KEY_URL);
        $data['if_ac'] = $if_ac;
        $data['maps_uid'] = $maps_uid;
        $data = json_encode($data);
        $result = $this->http_post($url, $data,false,'json');
        $result = json_decode($result, true);
        return $result;
    }

    public function PushFee($arr)
    {
        $if_ac = (($this->isDebug) ? self::IF_AC_TEST : self::IF_AC);
        $maps_uid = (($this->isDebug) ? self::MAPS_UID_TEST : self::MAPS_UID);
        $if_mod = (($this->isDebug) ? self::IF_MOD_TEST : self::IF_MOD);
        $url = (($this->isDebug) ? self::URL_TEST : self::URL);
        $key = $this->GetKey();
        if($key['status'] != 1) return ['status'=>'fail','msg'=>'KEY 异常'];
        $data['if_ac'] = $if_ac;
        $data['maps_uid'] = $maps_uid;
        $data['if_mod'] = $if_mod;
        natsort($data);
        $str = implode('',$data).$key['key'];
//        natcasesort($array2);

        $signature = strtoupper(sha1($str)) ;

        $data['signature'] = $signature;
        $ph_no = substr($arr['ph_no'],3);
        $data['if_content'] = [$ph_no.'`'.Data::$_express_type[$arr['express_type']].'`'.$arr['express_fee']];
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        $result = $this->http_post($url, $data,false,'json');
        $result = json_decode($result, true);

        if($result['status'] == 1){
            $result['status'] = 'success';
        }
        return $result;
    }


    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    private function http_post($url, $param, $post_file = false, $contenttype = '')
    {
        $timeout = 8;
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (empty($contenttype)) {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded'));
        } else if ($contenttype == "json") {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));

        }

        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }

        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($oCurl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($oCurl, CURLOPT_TIMEOUT, $timeout);

        $log = new Logging();
        $log->lfile('Pos_http_post');
        $log->lwrite(($url));
        //$log->lwrite((json_encode($param)));
        $log->lwrite(($strPOST));
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        $log->lwrite(($sContent));
        return $sContent;
    }
}